#include "kit/event/FollowEvent.hpp"
#include <string>
#include <vector>
#include "kit/tool/Tokenizer.hpp"
namespace KitEvent
{

	FollowEvent::~FollowEvent()
	{

	}
	void FollowEvent::execute(KitUser::UserMap * iUsers)
	{
		// if the user that is going to be followed exists
		if(this->isValid(iUsers))
		{
			// add a follower and send a message
			KitUser::User * aTo = iUsers->get(_message->getTo());
			aTo->addFollower(_message->getFrom());
			try
			{
				if(aTo->getConnection()->isValid())
					aTo->getConnection()->write(_message->getPayload());
			}
			catch(SystemSocket::WriteException & aException)
			{
				// if we couldn't write anymore on the connection mark it as invalid
				aTo->getConnection()->markInvalid();
			}
		}
	}

	bool FollowEvent::isValid(KitUser::UserMap * iUsers)
	{
		// it has to contain the user that should be informed
		return iUsers->contains(_message->getTo());
	}
}
